<template>
  <view class="order-page">
    <view class="order-header">
      <view class="order-tabs">
        <view 
          class="tab-item" 
          :class="{ active: activeTab === 'all' }"
          @click="switchTab('all')"
        >
          全部
        </view>
        <view 
          class="tab-item" 
          :class="{ active: activeTab === 'pending' }"
          @click="switchTab('pending')"
        >
          待付款
        </view>
        <view 
          class="tab-item" 
          :class="{ active: activeTab === 'paid' }"
          @click="switchTab('paid')"
        >
          已付款
        </view>
        <view 
          class="tab-item" 
          :class="{ active: activeTab === 'completed' }"
          @click="switchTab('completed')"
        >
          已完成
        </view>
      </view>
    </view>

    <scroll-view class="order-list" scroll-y>
      <view v-if="orderList.length === 0" class="empty-state">
        <view class="empty-icon">📋</view>
        <view class="empty-text">暂无订单</view>
      </view>
      
      <view v-else>
        <view 
          v-for="order in filteredOrders" 
          :key="order.id" 
          class="order-item"
          @click="goToOrderDetail(order.id)"
        >
          <view class="order-header-info">
            <view class="order-number">订单号：{{ order.orderNo }}</view>
            <view class="order-status" :class="order.status">{{ getStatusText(order.status) }}</view>
          </view>
          
          <view class="order-content">
            <image class="course-image" :src="order.courseImage" mode="aspectFill" />
            <view class="course-info">
              <view class="course-name">{{ order.courseName }}</view>
              <view class="course-teacher">讲师：{{ order.teacher }}</view>
              <view class="course-time">时间：{{ order.courseTime }}</view>
            </view>
          </view>
          
          <view class="order-footer">
            <view class="order-price">¥{{ order.price }}</view>
            <view class="order-actions">
              <button 
                v-if="order.status === 'pending'" 
                class="action-btn pay-btn"
                @click.stop="payOrder(order.id)"
              >
                立即支付
              </button>
              <button 
                v-if="order.status === 'paid'" 
                class="action-btn confirm-btn"
                @click.stop="confirmOrder(order.id)"
              >
                确认完成
              </button>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import Taro from '@tarojs/taro'

const activeTab = ref('all')
const orderList = ref([
  {
    id: 1,
    orderNo: '202312010001',
    courseName: '零基础学画画',
    teacher: '张老师',
    courseTime: '2023-12-15 14:00',
    price: 299,
    status: 'pending',
    courseImage: 'https://via.placeholder.com/80x80'
  },
  {
    id: 2,
    orderNo: '202312010002',
    courseName: '钢琴入门课程',
    teacher: '李老师',
    courseTime: '2023-12-16 10:00',
    price: 399,
    status: 'paid',
    courseImage: 'https://via.placeholder.com/80x80'
  },
  {
    id: 3,
    orderNo: '202312010003',
    courseName: '书法基础班',
    teacher: '王老师',
    courseTime: '2023-12-10 16:00',
    price: 199,
    status: 'completed',
    courseImage: 'https://via.placeholder.com/80x80'
  }
])

const filteredOrders = computed(() => {
  if (activeTab.value === 'all') {
    return orderList.value
  }
  return orderList.value.filter(order => order.status === activeTab.value)
})

const switchTab = (tab) => {
  activeTab.value = tab
}

const getStatusText = (status) => {
  const statusMap = {
    pending: '待付款',
    paid: '已付款',
    completed: '已完成',
    cancelled: '已取消'
  }
  return statusMap[status] || '未知状态'
}

const goToOrderDetail = (orderId) => {
  Taro.navigateTo({
    url: `/pages/order/detail?id=${orderId}`
  })
}

const payOrder = (orderId) => {
  Taro.showToast({
    title: '跳转支付页面',
    icon: 'none'
  })
}

const confirmOrder = (orderId) => {
  Taro.showModal({
    title: '确认完成',
    content: '确认课程已完成？',
    success: (res) => {
      if (res.confirm) {
        const order = orderList.value.find(item => item.id === orderId)
        if (order) {
          order.status = 'completed'
        }
        Taro.showToast({
          title: '操作成功',
          icon: 'success'
        })
      }
    }
  })
}

onMounted(() => {
  // 页面加载时可以获取订单数据
})
</script>

<style lang="less">
.order-page {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.order-header {
  background-color: #fff;
  padding: 0;
}

.order-tabs {
  display: flex;
  align-items: center;
  
  .tab-item {
    flex: 1;
    text-align: center;
    padding: 32rpx 0;
    font-size: 28rpx;
    color: #666;
    position: relative;
    
    &.active {
      color: #3478F6;
      font-weight: 500;
      
      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 60rpx;
        height: 4rpx;
        background-color: #3478F6;
        border-radius: 2rpx;
      }
    }
  }
}

.order-list {
  height: calc(100vh - 120rpx);
  padding: 20rpx;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 200rpx 0;
  
  .empty-icon {
    font-size: 120rpx;
    margin-bottom: 20rpx;
  }
  
  .empty-text {
    font-size: 28rpx;
    color: #999;
  }
}

.order-item {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 24rpx;
  margin-bottom: 20rpx;
  
  .order-header-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20rpx;
    
    .order-number {
      font-size: 24rpx;
      color: #999;
    }
    
    .order-status {
      font-size: 24rpx;
      padding: 8rpx 16rpx;
      border-radius: 12rpx;
      
      &.pending {
        color: #ff6b35;
        background-color: #fff2e8;
      }
      
      &.paid {
        color: #3478F6;
        background-color: #e8f2ff;
      }
      
      &.completed {
        color: #00c851;
        background-color: #e8f8f0;
      }
    }
  }
  
  .order-content {
    display: flex;
    margin-bottom: 20rpx;
    
    .course-image {
      width: 120rpx;
      height: 120rpx;
      border-radius: 12rpx;
      margin-right: 20rpx;
    }
    
    .course-info {
      flex: 1;
      
      .course-name {
        font-size: 32rpx;
        font-weight: 500;
        color: #333;
        margin-bottom: 12rpx;
      }
      
      .course-teacher,
      .course-time {
        font-size: 24rpx;
        color: #666;
        margin-bottom: 8rpx;
      }
    }
  }
  
  .order-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .order-price {
      font-size: 32rpx;
      font-weight: 600;
      color: #ff6b35;
    }
    
    .order-actions {
      display: flex;
      gap: 20rpx;
      
      .action-btn {
        padding: 16rpx 32rpx;
        border-radius: 24rpx;
        font-size: 24rpx;
        border: none;
        
        &.pay-btn {
          background-color: #3478F6;
          color: #fff;
        }
        
        &.confirm-btn {
          background-color: #00c851;
          color: #fff;
        }
      }
    }
  }
}
</style>